package cb.com.etcdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cb.com.etcdemo.API.API;
import cb.com.etcdemo.bean.Devicse;
import cb.com.etcdemo.http.Okhttp;
import cb.com.etcdemo.util.JSONUtils;
import cb.com.etcdemohgz.R;

public class DevicesActivity extends AppCompatActivity {

    @BindView(R.id.et_completed_search)
    EditText etCompletedSearch;
    @BindView(R.id.iv_completed_search)
    ImageView ivCompletedSearch;
    @BindView(R.id.ll_complete_searxh)
    LinearLayout llCompleteSearxh;
    @BindView(R.id.tv_stationName)
    TextView tvStationName;
    @BindView(R.id.tv_deviceIp)
    TextView tvDeviceIp;
    @BindView(R.id.tv_currentVersion)
    TextView tvCurrentVersion;
    @BindView(R.id.tv_offLineMinute)
    TextView tvOffLineMinute;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_oNLine)
    TextView tv_oNLine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devices);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.iv_completed_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.iv_completed_search:
                String s = etCompletedSearch.getText().toString();
                if (TextUtils.isEmpty( s)) {
                    Toast.makeText(this, "请输入设备号！", Toast.LENGTH_SHORT).show();
                  return;
                }
                API.getxinxi(s, new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("8888", "xx"+st);
                        Devicse devicse = JSONUtils.parseJSON(st, Devicse.class);
                        List<Devicse.ListBean> list = devicse.getList();
                        if (list!=null&&list.size()>0) {
                            tvDeviceIp.setText("设备IP:"+list.get(0).getDeviceIp());
                            tvCurrentVersion.setText("设备版本"+list.get(0).getCurrentVersion());
                            tvOffLineMinute.setText("离线时间"+list.get(0).getOffLineMinute());
                            if (list.get(0).getOffLineMinute()<=6) {
                                tv_oNLine.setText("在线");
                            } else {
                                tv_oNLine.setText("离线");
                            }
                        } else {
                            Toast.makeText(DevicesActivity.this, "设备号不存在！", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Log.e("8888", "errst"+errst);
                    }
                });
                break;
        }
    }
}
